package com.szl.yeb.controller;


import com.szl.yeb.common.constant.MsgConstant;
import com.szl.yeb.entity.Position;
import com.szl.yeb.common.R;
import com.szl.yeb.service.PositionService;
import com.szl.yeb.utils.ResponseProcessor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * (Position)表控制层
 *
 * @author szl
 * @since 2022-08-24 16:21:27
 */
@Api(tags = "职位管理接口(PositionController)")
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    private PositionService positionService;


    @ApiOperation("获取所有职位信息")
    @GetMapping("/")
    public R<Object> getAllPositions() {
        return R.obj(positionService.list());
    }


    @ApiOperation("添加职位信息")
    @PostMapping("/")
    public R<Object> addPosition(@RequestBody Position position) {
        if (position.getName() == null || position.getName().trim().isEmpty()) {
            return R.errMsg("职位不能为空");
        }
        return ResponseProcessor.getResWithMsg(positionService.save(position), MsgConstant.ADD);
    }

    @ApiOperation(value = "更新职位信息")
    @PutMapping("/")
    public R<Object> updatePosition(@RequestBody Position position) {
        return ResponseProcessor.getResWithMsg(positionService.updateById(position), MsgConstant.UPDATE);
    }

    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/{id}")
    public R<Object> deletePosition(@PathVariable Integer id) {
        return ResponseProcessor.getResWithMsg(positionService.removeById(id), MsgConstant.DELETE);
    }

    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/")
    public R<Object> deletePositions(@RequestParam List<Integer> ids) {
        //若用List接收数组，要加@RequestParam注解
        return ResponseProcessor.getResWithMsg(positionService.removeBatchByIds(ids), MsgConstant.DELETE);
    }
}

